//
//  CourseArrangementVc.m
//  Courses
//
//  Created by xxx on 15/1/15.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "CourseArrangementVc.h"
#import "CourseClass.h"
#import "CourseArrangementCell.h"

#import "UIImageView+WebCache.h"
#import "NSDate+WTF.h"
#import "VipCourseClass.h"
@implementation CourseArrangementVc

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"我的高级课";
    
    

    [self addTeacherTitleView];
    
    [self loadRequest];
}

#pragma mark - teacher title view

static const CGFloat kAvatarWidth = 80;
static const CGFloat kAvatarHeight = 60;
static const CGFloat kPaddingX = 15;
static const CGFloat kPaddingY = 15;

- (void)addTeacherTitleView
{
    CGFloat kTeacherTitileViewHeight = kAvatarHeight + kPaddingY * 2;
    
    UIView *view = [[UIView alloc] initWithFrame:AutoRectMake(0, 0, AutoFloat(DEFAULT_WIDTH), kTeacherTitileViewHeight, WTFAutoLayoutTypeBar)];
    
    // avatar
    UIImageView *imgViewAvatar = [[UIImageView alloc] initWithFrame:AutoRectMake(kPaddingX, kPaddingY, kAvatarWidth, kAvatarHeight, WTFAutoLayoutTypeRaw)];
    imgViewAvatar.contentMode = UIViewContentModeScaleAspectFit;
    imgViewAvatar.layer.cornerRadius = 18;
    imgViewAvatar.layer.masksToBounds = YES;
    [view addSubview:imgViewAvatar];
    
    // course name
    UILabel *lblCourseName = [[UILabel alloc] initWithFrame:AutoRectMake(kAvatarWidth + kPaddingX * 2, 0, AutoFloat(180), kTeacherTitileViewHeight, WTFAutoLayoutTypeRaw)];
    [view addSubview:lblCourseName];
    
    if (_course)
    {
        [imgViewAvatar sd_setImageWithURL:[NSURL URLWithString:_course.teacherHeadPhotoUrl]];
        lblCourseName.text = _course.courseName;
    }
    
    self.tableView.tableHeaderView = view;
}

#pragma mark - table

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"高级课程表%d",self.items.count);
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section >= self.items.count)
        return 0;
    
    id courseClass = self.items[section];
    
    if ([courseClass isKindOfClass:[CourseClass class]])
    {
        return [courseClass lessons].count;
    }
    return 0;
}

- (NSArray *)fetchedItems
{
    return [CourseClass courseArrangementByCourseId:self.course.courseId];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseArrangementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[CourseArrangementCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    if (self.items.count > indexPath.section)
    {
        if ([self.items[indexPath.section] isKindOfClass:[CourseClass class]])
        {
            CourseClass *courseClass = self.items[indexPath.section];
            if (courseClass.lessons.count > indexPath.row)
            {
                if ([courseClass.lessons[indexPath.row] isKindOfClass:[Lesson class]])
                {
                    cell.index = indexPath.row;
                    cell.lesson = courseClass.lessons[indexPath.row];
                }
            }
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *backgroundView = [[UIView alloc] initWithFrame:AutoRectMake(0, 0, DEFAULT_WIDTH, 30, WTFAutoLayoutTypeBar)];
    backgroundView.backgroundColor = [WTFColors defaultBarColor];
    
    UILabel *lbl = [[UILabel alloc] initWithFrame:backgroundView.frame];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.textColor = [UIColor whiteColor];
    [backgroundView addSubview:lbl];
    
    if (self.items.count > section)
    {
        lbl.text = [NSDate monthStringForDateString:[self.items[section] lessonBeginDate]];
    }
    
    return backgroundView;
}

@end
